<%-- 
    Document   : verTelefono
    Created on : 08-jun-2009, 16:34:25
    Author     : Alberto
--%>

<%@page contentType="text/html" pageEncoding="UTF-8" session="false" import="dao.*, domain.*, planner.*, java.util.*, java.io.File, mobile.*, turnover.*, plan.PlanFactory"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>DOMECO</title>
        <link href="style.css" rel="stylesheet" type="text/css">
    </head>
    <body>
        <%if (request.getSession(false) != null) {%>
        <table align="center" width="1000" >
            <tr>
                <td>
                    <div id="menu" align="center"><jsp:include page="./menu principal.jsp"></jsp:include></div>
                    <br>
                    <p id="campoAzul" align="center" valign="middle" width="235px"><span id="blanco">Telefonía</span><br></p>

                    <div id="cuenta" align="left">
                        <span id="azulTitulo">Datos del teléfono</span>
                        <br>
                        <br>
                        <%
    Mobilecontract mc = new Operations().getMobileContract(Integer.parseInt(request.getParameter("idCont")));
    List list = new Operations().getMobileconsumptionList(mc);
    List listDate = new ArrayList();
    Iterator it = list.iterator();
    while (it.hasNext()) {
        Mobileconsumption m = (Mobileconsumption) it.next();
        if (m.getDateFrom().after(new java.util.Date(new java.util.Date().getTime() - 15634800000l))) {
            listDate.add(m);
        }
        mc.getContract().getDateContract().before(new java.util.Date());
    }
                        %>


                        <table id="tabla">
                            <tr id="campoAzul" align="center">
                                <td width="200px"><span id="blanco">Número de Teléfono</span></td>
                                <td width="200px"><span id="blanco">Compañia</span></td>
                                <td><span id="blanco" >Permanencia restante</span></td>
                            </tr>
                            <%
    //Fecha del contrato
    int day = mc.getContract().getDateContract().getDate();
    int month = mc.getContract().getDateContract().getMonth();
    int year = mc.getContract().getDateContract().getYear();
    //Pasamos a String la fecha
    String f = year + "-" + month + "-" + day;

    //Calculamos la fecha actual
    long actual = new java.sql.Date((new java.util.Date().getTime())).getTime();
    //Calculamos la fecha del contrato
    long fCont = new java.sql.Date(year, month, day).getTime();

    //Restamos a la permanencia el cociente actual-fCont

                            %>

                            <tr id="datosCuenta" align="center">
                                <td><span><%=mc.getMobileNumber()%></span></td>
                                <td><span><%=mc.getCompany()%></span></td>
                                <td><span><%if (new Date(mc.getContract().getDateContract().getTime() + mc.getPermanence() * 2605800000l).after(new java.util.Date())) {
        out.print(mc.getPermanence() - ((actual - fCont) / 2605800000l) + " meses restantes");
    } else {%>Contrato terminado<%}%></span></td>
                        </tr></table>


                        <br>
                        <p id="campoAzul" align="center" valign="middle" width="235px"><span id="blanco">Recomendaciones sobre éste Teléfono</span><br></p>

                        <%List<Mobileconsumption> listMC = new Operations().getListMobileconsumptionDate3(mc);
    if (listMC.size() >= 3) {
        Iterator itMC = listMC.iterator();
        List<Call> callList = new LinkedList();
        List<Message> messList = new LinkedList();
        while (itMC.hasNext()) {
            Mobileconsumption mcc = (Mobileconsumption) itMC.next();
            String url = mcc.getUrl();
            File file = new File(url);
            NewFile nf = new NewFile(file, mcc);
            callList.addAll(nf.getCallList());
            messList.addAll(nf.getMessList());
        }
        MobileConsumption mccc = new MobileConsumption(0, 0, callList.size(), messList.size(), 0, null, null);
        turnover.Consumption cons = (turnover.Consumption) mccc;
        PlanFactory planF = new PlanFactory(mc.getCompany(), cons);
        Map map = planF.getCombinations();
        double min = Double.MAX_VALUE;
        String mejorPlan = "";
        Iterator itm = map.keySet().iterator();
        while (itm.hasNext()) {
            String plan = (String) itm.next();
            Double total = (Double) map.get(plan);
            if (total < min) {
                min = total;
                mejorPlan = plan;
            }
        }
                        %>

                        <span id="datosCuenta">La mejor tarifa para la media de sus últimas facturas es:</span><br>
                        <span id="rojo"><%out.print(mejorPlan);%></span><br>
                        <span style="color:navy">El coste sería de: <%out.print(min);%> €</span>


                        <%
                        } else {
                        %><p id="rojo" align="center"><%
                            out.print("No tiene suficientes facturas para realizar una recomendación fiable.");
                        %></p><%
    }

    List<Mobileconsumption> listMC6 = new Operations().getListMobileconsumptionDate6(mc);
    Collections.sort(listMC6);
    Iterator itMC6 = listMC6.iterator();
                        %>

                        <table id="tabla" align="center">
                            <td>
                                <tr>

                                    <img align="right" src="http://chart.apis.google.com/chart?chs=350x150&chd=t:<%
    List<String> meses = new LinkedList();
    Map mapMeses = new HashMap();
    mapMeses.put(1, "Enero");
    mapMeses.put(2, "Febrero");
    mapMeses.put(3, "Marzo");
    mapMeses.put(4, "Abril");
    mapMeses.put(5, "Mayo");
    mapMeses.put(6, "Junio");
    mapMeses.put(7, "Julio");
    mapMeses.put(8, "Agosto");
    mapMeses.put(9, "Septiembre");
    mapMeses.put(10, "Octubre");
    mapMeses.put(11, "Noviembre");
    mapMeses.put(12, "Diciembre");
    while (itMC6.hasNext()) {
        Mobileconsumption mc6 = (Mobileconsumption) itMC6.next();
        if (itMC6.hasNext()) {
            out.print(mc6.getInvoice().getAmount() + ",");
        } else {
            out.print(mc6.getInvoice().getAmount());
        }
        String mes = (String) mapMeses.get(mc6.getDateFrom().getMonth() + 1);
        meses.add(mes);
    }
                                         %>&cht=bvs&chl=<%
    Iterator itMes = meses.iterator();
    while (itMes.hasNext()) {
        String messs = (String) itMes.next();
        if (itMes.hasNext()) {
            out.print(messs + "|");
        } else {
            out.print(messs);
        }

    }
                                         %>&chco=<%
    if (mc.getCompany().equals("Orange")) {
        out.print("ff9933");
    } else if (mc.getCompany().equals("Vodafone")) {
        out.print("cc0000");
    } else if (mc.getCompany().equals("Movistar")) {
        out.print("3366ff");
    }

                                         %>,00FF00&chbh=50"alt="Estadistica del telefono" />

                                </tr>
                            </td>
                        </table>





                    </div>
                    <div id="barraDer">


                        <div id="accesos">
                            <table align="center">
                                <tr ><td id="campoAzul" align="center" valign="middle" width="235px"><span id="blanco">Datos actuales</span><br></td></tr>
                            </table>

                            <p align="center"><a href="./nuevoContTelefono.jsp" id="azul"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>A&ntilde;adir teléfono</span></a></p>
                            <p align="center"> <a href="./facturaMovil.jsp?error=false" id="azul"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>A&ntilde;adir factura</span></a> </p>
                            <p align="center">  <a href="./modBorContratoTelefonia.jsp" id="colorEvento"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>Modificar/Borrar contrato</span></a></p>
                        </div>


                    </div>
                    

                </td>

            </tr>
            <tr><td>              <div id="abajo" align="center"><p>&copy;   Domeco &mdash; Proyecto Informático A&ntilde;o 2009 </p></div></td></tr>

        </table>
        <%} else {%>
        <jsp:forward page="./index.jsp"></jsp:forward>
        <%}%>
    </body>
</html>
